package com.saku.uiprojecttest.activities;/**
 * Created by liumin on 16/11/15.
 */


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saku.uiprojecttest.R;
import com.saku.uiprojecttest.bean.PagerItem;

import java.util.List;

/**
 * User: Missa
 * Date: 2016-11-15
 * Time: 23:24
 * Description:
 */
public class CarPagerAdapter extends PagerAdapter {
    private List<PagerItem> list;
    private final Context mContext;

    public CarPagerAdapter(Context context, List<PagerItem> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        int pagerCount = (int) ((list.size() / 2.0f) + 0.5);
        return pagerCount;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, null);
        final ImageView iv = (ImageView) view.findViewById(R.id.imageview);
        final ImageView ivRight = (ImageView) view.findViewById(R.id.imageview_right);
        final TextView tvLeft = (TextView) view.findViewById(R.id.tvLeft);
        final TextView tvRight = (TextView) view.findViewById(R.id.tvRight);

        int leftPos = position * 2;
        if (leftPos < list.size()) {
            iv.setImageResource(list.get(leftPos).resId);
            tvLeft.setText(list.get(leftPos).desc);
        }

        if (leftPos + 1 < list.size()) {
            ivRight.setImageResource(list.get(leftPos + 1).resId);
            tvRight.setText(list.get(leftPos + 1).desc);
        }

        container.addView(view);
        return view;
    }

    @Override
    public float getPageWidth(int position) {
        return 1f;
    }

}
